﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMSModel;
using MainQuoteDBModel;
using CMSClient;

/// <summary>
///CommonBll 的摘要说明
/// </summary>
public class CommonBll
{
    private static Dictionary<string, string> dicStocks;

    public static Dictionary<string, string> DicStocks
    {
        get
        {
            if (dicStocks != null)
            {
                return dicStocks;
            }
            MainQuoteDBEntities db = new MainQuoteDBEntities();
            var data= db.TB_GGXX.Select(x => new { x.G_CODE, x.G_NAME });
            dicStocks = new Dictionary<string, string>();
            foreach(var c in data)
            {
                if (!dicStocks.Keys.Contains(c.G_CODE) && !c.G_CODE.StartsWith("4"))
                {
                    dicStocks.Add(c.G_CODE, c.G_NAME);
                }
            }
            return dicStocks;
        }
        set
        {
            dicStocks = value;
        }
    }

    public static  void SetValue(object o, string p, object value)
    {
        var property = o.GetType().GetProperty(p);
        var getValue = property.GetValue(o, null);
        if (getValue != null && value != null)
        {
            if (property.GetValue(o, null).ToString() != value.ToString())
            {
                property.SetValue(o, value, null);
            }
        }
        else
        {
            property.SetValue(o, value, null);
        }
    }

    /// <summary>
    /// 新闻中需要替换的代码
    /// </summary>
    public static List<string[]> CodeLst
    {
        get
        {
            string key = "CommonBll_CodeLst";
            var tlst= (List<string[]>)System.Web.HttpRuntime.Cache[key];
            if (tlst != null && tlst.Count > 0)
            {
                return tlst;
            }
            Client client = new Client();
            string str= client.GetClientBySocket("http://172.28.1.90/stock/HashStock2.ashx");
            var lst= Newtonsoft.Json.JsonConvert.DeserializeObject<List<TempStock>>(str);
            tlst = lst.Select(x => new string[] { x.N, x.C,x.F }).ToList();
            if (tlst != null && tlst.Count > 0)
            {
                System.Web.HttpRuntime.Cache.Add(key, tlst, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
            }
            return tlst;
        }
    }

    class TempStock
    {
        /// <summary>
        /// 股票代码
        /// </summary>
        public string C
        {
            get;
            set;
        }

        /// <summary>
        /// 股票名称
        /// </summary>
        public string N
        {
            get;
            set;
        }

        /// <summary>
        /// 类型状态
        /// </summary>
        public string F
        {
            get;
            set;
        }

        /// <summary>
        /// 类型名称
        /// </summary>
        public string T
        {
            get;
            set;
        }

        /// <summary>
        /// 股票状态是否正常
        /// </summary>
        public string S
        {
            get;
            set;
        }
    }
}